<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>


<script>
    // 1. 差集
    /**
     * const a = [1,2,3]
     * const b = [2,3,4,5]
     * return [1,4,5]
     */
    const diffArray = function (a, b) {
        a.sort((m,n)=>{
            return m-n
        })
        b.sort((j,k)=>{
            return j-k
        })
        let i = 0
        let j = 0
        const result = []
        while (i<a.length && j<b.length){
            if(a[i] === b[j]){
                i++
                j++
            }else if(a[i]<b[j]){
                result.push(a[i])
                i++
            }else if(a[i]>b[j]){
                result.push(b[j])
                j++
            }
        }
        if(i<a.length){
            result.push(...a.slice(i))
        }
        if(j<b.length){
            result.push(...b.slice(j))
        }
        console.log(result)
        return result
    }
    diffArray([1,2,3],[2,3,4,5])
    diffArray([4,5,6],[2,3,4,5])
    diffArray([1,9,2,8,4,3],[7,9,8,2,3,4,5])




    // 2.一个数组先升序后降序,找出其中最大值
    /**
     *
     * @param arr
     * @returns {*}
     */
    function maxNum(arr) {
        var i = Math.floor(arr.length/2)
        var j = i+1
        var k = j+1
        while (k<arr.length-1 && i>0){
            if(arr[i]<arr[j]&&arr[j]<arr[k]){
                i++
                j++
                k++
            }else if(arr[i]>arr[j]&&arr[j]>arr[k]){
                i--
                j--
                k--
            }else if (arr[i]<arr[j] && arr[j]>arr[k]){
                console.log(arr[j])
                return arr[j]
            }
        }
    }
    maxNum([1,3,5,7,9,6,4,2])
    /*
    * 筛选出符合的内容
    * let arr = [1,4,2,7,9,3,8]
    * 返回出数组中大于等于5的元素
    * 从最后一个开始遍历到第一个结束
    */
   let arr = [1,4,2,7,9,3,8]
    function filterArr(arr) {
        for(let i = arr.length-1;i >= 0;i--){
            if(arr[i]<5){
            arr.splice(i,1)
        }
    }
        return arr
    }
    console.log(filterArr(arr));
   
</script>
</body>
</html>


